package com.efast.dmom.mgmt.openws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cafeauto.ws.Message;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.base.WSAbstractAdapter;
import com.efast.cafe.commponent.es.webservice.utils.W3CElementUtil;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.mes.testline.BreakData;
import com.efast.dmom.mgmt.bean.mes.testline.BreakDataBean;
import com.efast.dmom.mgmt.bean.mes.testline.BreakDataBizContent;
import com.efast.dmom.mgmt.bean.mes.testline.BreakInboundDataSet;
import com.efast.dmom.mgmt.bean.mes.testline.TestLineDataLogBean;
import com.efast.dmom.mgmt.service.mes.testline.impl.TestLineService;

@CafeWSService(bizCode = "Break_Test")
public class BreakDataService extends WSAbstractAdapter {

	@Override
	public Message handleMessage(Message message) {

		logger.info("Received Break data from: " + message.getBIZ_CODE());

		BreakDataBizContent content = W3CElementUtil.elementUnmarshalToObj(message.getBIZ_CONTENT(),
				BreakDataBizContent.class);
		BreakInboundDataSet inboundData = content.getBreakInboundDataSet();
		List<BreakData> dataList = inboundData.getBreakData();

		if(dataList != null && dataList.size() >0){
			
			List<BreakDataBean> list = new ArrayList<BreakDataBean>();
			
			for (BreakData data : dataList) {

				BreakDataBean dataBean = new BreakDataBean();

				dataBean.setVin(data.getVin());
				dataBean.setBrakingResult(data.getBrakingResult());
				dataBean.setOneAxisAxleBrakingRate(data.getOneAxisAxleBrakingRate());
				dataBean.setOneAxisBrakingRate(data.getOneAxisBrakingRate());
				dataBean.setOneAxisImbalanceRate(data.getOneAxisImbalanceRate());
				dataBean.setOneAxisjzbphRate(data.getOneAxisjzbphRate());
				dataBean.setOneAxisLeftCoordinationTime(data.getOneAxisLeftCoordinationTime());
				dataBean.setOneAxisLeftKg(data.getOneAxisLeftKg());
				dataBean.setOneAxisLeftRetardationRate(data.getOneAxisLeftRetardationRate());
				dataBean.setOneAxisLoadingAxleLoad(data.getOneAxisLoadingAxleLoad());
				dataBean.setOneAxisMaxLeftN(data.getOneAxisMaxLeftN());
				dataBean.setOneAxisMaxLeftValueN(data.getOneAxisMaxLeftValueN());
				dataBean.setOneAxisRightCoordinationTime(data.getOneAxisRightCoordinationTime());
				dataBean.setOneAxisRightKg(data.getOneAxisRightKg());
				dataBean.setOneAxisRightRetardationRate(data.getOneAxisRightRetardationRate());
				dataBean.setOneAxleProjectResult(data.getOneAxleProjectResult());
				dataBean.setTime(data.getTime());
				dataBean.setTwoAxisAxleBrakingRate(data.getTwoAxisAxleBrakingRate());
				dataBean.setTwoAxisBrakingRate(data.getTwoAxisBrakingRate());
				dataBean.setTwoAxisImbalanceRate(data.getTwoAxisImbalanceRate());
				dataBean.setTwoAxisjzbphRate(data.getTwoAxisjzbphRate());
				dataBean.setTwoAxisLeftCoordinationTime(data.getTwoAxisLeftCoordinationTime());
				dataBean.setTwoAxisLeftKg(data.getTwoAxisLeftKg());
				dataBean.setTwoAxisLeftRetardationRate(data.getTwoAxisLeftRetardationRate());
				dataBean.setTwoAxisLoadingAxleLoad(data.getTwoAxisLoadingAxleLoad());
				dataBean.setTwoAxisMaxLeftN(data.getTwoAxisMaxLeftN());
				dataBean.setTwoAxisMaxLeftValueN(data.getTwoAxisMaxLeftValueN());
				dataBean.setTwoAxisMaxRightN(data.getTwoAxisMaxRightN());
				dataBean.setTwoAxisMaxRightValueN(data.getTwoAxisMaxRightValueN());
				dataBean.setTwoAxisRightCoordinationTime(data.getTwoAxisRightCoordinationTime());
				dataBean.setTwoAxisRightKg(data.getTwoAxisRightKg());
				dataBean.setTwoAxisRightRetardationRate(data.getTwoAxisRightRetardationRate());
				dataBean.setTwoAxleProjectResult(data.getTwoAxleProjectResult());
				dataBean.setParkingBrakingRate(data.getParkingBrakingRate());
				dataBean.setParkingKg(data.getParkingKg());
				dataBean.setParkingMaxNLeft(data.getParkingMaxNLeft());
				dataBean.setParkingMaxNRight(data.getParkingMaxNRight());
				dataBean.setParkingProjectResult(data.getParkingProjectResult());
				dataBean.setVehicleBrakingRate(data.getVehicleBrakingRate());
				dataBean.setOneAxisMaxRightN(data.getOneAxisMaxRightN());
				dataBean.setVehicleMaxN(data.getVehicleMaxN());
				dataBean.setVehicleProjectResult(data.getVehicleProjectResult());
				dataBean.setOneAxisMaxRightValueN(data.getOneAxisMaxRightValueN());
				dataBean.setCreateTime(new Date());
				dataBean.setCreateUser(message.getBIZ_CODE());
				dataBean.setTestNumber(data.getTestNumber());

				list.add(dataBean);
			}
			TestLineService service = SpringContextUtils.getBean(TestLineService.class);
			service.insertBreakData(list);
			
			TestLineDataLogBean logBean = new TestLineDataLogBean();
			logBean.setVin(dataList.get(0).getVin());
			logBean.setStatus("保存");
			logBean.setReceivedTime(new Date());
			logBean.setSource(message.getMESSAGE_FROM());
			logBean.setDestination(message.getMESSAGE_TO());
			
			service.insertTestLineLog(logBean);
		}

		message.setBIZ_CONTENT(null);
		message.setMESSAGE_FROM("MES");
		message.setMESSAGE_TO("Break_Test");
		message.setRET_CODE("200");
		message.setRET_CODE_DESC("Successful");
		return message;
	}

}
